package com.youlai.boot.system.controller;

import com.alibaba.excel.EasyExcel;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.system.model.form.SpotRecordForm;
import com.youlai.boot.system.model.query.SpotRecordQuery;
import com.youlai.boot.system.model.vo.SpotCountDTO;
import com.youlai.boot.system.model.vo.SpotCountExcelVO;
import com.youlai.boot.system.model.vo.SpotRecordExcelVO;
import com.youlai.boot.system.service.SpotCountService;
import com.youlai.boot.util.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.SpotCountForm;
import com.youlai.boot.system.model.query.SpotCountQuery;
import com.youlai.boot.system.model.vo.SpotCountVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.List;

/**
 * 统计前端控制层
 *
 * @author lwb
 * @since 2025-07-01 22:00
 */
@Tag(name = "统计接口")
@RestController
@RequestMapping("/api/v1/spotCounts")
@RequiredArgsConstructor
public class SpotCountController  {

    private final SpotCountService spotCountService;

    @Operation(summary = "统计分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:spotCount:query')")
    public PageResult<SpotCountVO> getSpotCountPage(SpotCountQuery queryParams ) {
        IPage<SpotCountVO> result = spotCountService.getSpotCountPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "导出统计")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPerm('system:spotCount:word')")
    public void export(SpotCountQuery queryParams, HttpServletResponse response) throws IOException {

        List<SpotCountExcelVO> list = spotCountService.export(queryParams);
        InputStream resourceAsStream = ExcelUtils.class.getClassLoader().getResourceAsStream("excel-template/spotCount.xlsx");
        // 设置 header 和 contentType。写在最后的原因是，避免报错时，响应 contentType 已经被修改了
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(String.format("统计数据_%s.xlsx", LocalDate.now()), StandardCharsets.UTF_8));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        EasyExcel.write(response.getOutputStream()).withTemplate(resourceAsStream).sheet().doFill(list);
    }

    //@Operation(summary = "统计数据")
    @GetMapping("/count")
    @PreAuthorize("@ss.hasPerm('system:spotCount:query')")
    public Result<List<SpotCountDTO>> count(SpotCountQuery queryParams) {
        return Result.success(spotCountService.getCount(queryParams));
    }

//    @Operation(summary = "新增统计")
//    @PostMapping
//    @PreAuthorize("@ss.hasPerm('system:spotCount:add')")
//    public Result<Void> saveSpotCount(@RequestBody @Valid SpotCountForm formData ) {
//        boolean result = spotCountService.saveSpotCount(formData);
//        return Result.judge(result);
//    }

    @Operation(summary = "获取统计表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:spotCount:edit')")
    public Result<SpotCountForm> getSpotCountForm(
        @Parameter(description = "统计ID") @PathVariable Long id
    ) {
        SpotCountForm formData = spotCountService.getSpotCountFormData(id);
        return Result.success(formData);
    }

/*    @Operation(summary = "修改统计")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:spotCount:edit')")
    public Result<Void> updateSpotCount(
            @Parameter(description = "统计ID") @PathVariable Long id,
            @RequestBody @Validated SpotCountForm formData
    ) {
        boolean result = spotCountService.updateSpotCount(id, formData);
        return Result.judge(result);
    }*/

    @Operation(summary = "删除统计")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:spotCount:delete')")
    @Log(value = "删除统计", module = LogModuleEnum.OTHER)
    public Result<Void> deleteSpotCounts(
        @Parameter(description = "统计ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = spotCountService.deleteSpotCounts(ids);
        return Result.judge(result);
    }
}
